package com.mycompany.crm.controller;

import com.mycompany.crm.annotation.LogAnno;
import com.mycompany.crm.annotation.RequiredPermission;
import com.mycompany.crm.base.BaseController;
import com.mycompany.crm.base.ResultInfo;
import com.mycompany.crm.enums.StateStatus;
import com.mycompany.crm.query.SaleChanceQuery;
import com.mycompany.crm.service.SaleChanceService;
import com.mycompany.crm.utils.CookieUtil;
import com.mycompany.crm.utils.LoginUserUtil;
import com.mycompany.crm.vo.SaleChance;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author jy
 * @version 1.0
 * @date 2022/1/7 14:16
 */
@Controller
@RequestMapping("sale_chance")
public class SaleChanceController extends BaseController {

    @Resource
    private SaleChanceService saleChanceService;

    /**
     * 营销机会数据查询(分页多条件) 101001
     * 如果flag的值不为空且为1，则表示当前查询的是客户开发计划；否则查询营销机会数据
     *
     * @param saleChanceQuery
     * @return
     */
    @LogAnno(description = "营销机会数据查询(分页多条件) ")
    @RequiredPermission(code = "101001")
    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery,Integer flag,HttpServletRequest request){
        //判断flag的值
        if(flag != null && flag == 1){
            //查询客户开发计划
            //设置分配状态
            saleChanceQuery.setState(StateStatus.STATED.getType());
            //设置指派人,指派人为当前登录用户Id，所以要从cookie中去取
            Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
            saleChanceQuery.setAssignMan(userId);
        }

        return saleChanceService.querySaleChanceByParams(saleChanceQuery);
    }

    /**
     * 进入营销机会管理的页面 1010
     * @return
     */
    @LogAnno(description = "进入营销机会管理的页面")
    @RequiredPermission(code = "1010")
    @RequestMapping("index")
    public String index(){
        return "saleChance/sale_chance";
    }


    /**
     * 添加营销机会 101002
     * @param saleChance
     * @param request
     * @return
     */
    @LogAnno(description = "添加营销机会")
    @RequiredPermission(code = "101002")
    @PostMapping("add")
    @ResponseBody
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request){
        //从Cookie中拿到当前登录用户名
        String userName = CookieUtil.getCookieValue(request, "userName");
        //设置当前登录用户名到待添加的营销对象的创建人字段中
        saleChance.setCreateMan(userName);
        //调用Service层的添加方法
        saleChanceService.addSaleChance(saleChance);
        return success("营销机会数据添加成功！");
    }

    /**
     * 进入添加或修改营销机会页面
     * @return
     */
    @LogAnno(description = "进入添加或修改营销机会页面")
    @RequestMapping("toSaleChancePage")
    public String toSaleChancePage(Integer saleChanceId,HttpServletRequest request){

        //判断saleChanceId是否为空
        if (saleChanceId != null){
            //通过Id查询营销机会数据便于回显
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(saleChanceId);
            //将数据设置到请求域中
            request.setAttribute("saleChance",saleChance);
        }
        return "saleChance/add_update";
    }

    /**
     * 更新营销机会 101004
     * @param saleChance
     * @return
     */
    @LogAnno(description = "更新营销机会")
    @RequiredPermission(code = "101004")
    @PostMapping("update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance){
        //调用Service层的添加方法
        saleChanceService.updateSaleChance(saleChance);
        return success("营销机会数据更新成功！");
    }

    /**
     * 删除营销机会 101003
     * @param ids
     * @return
     */
    @LogAnno(description = "删除营销机会")
    @RequiredPermission(code = "101003")
    @PostMapping("delete")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids){
        //删除营销机会的数据
        saleChanceService.deleteSaleChance(ids);
        return success("营销机会数据删除成功！");
    }

    /**
     * 更新营销机会的开发状态
     * @param id
     * @param devResult
     * @return
     */
    @LogAnno(description = "更新营销机会的开发状态")
    @PostMapping("updateSaleChanceDevResult")
    @ResponseBody
    public ResultInfo updateSaleChanceDevResult(Integer id,Integer devResult){
        //删除营销机会的数据
        saleChanceService.updateSaleChanceDevResult(id,devResult);
        return success("开发状态更新成功！");
    }

}
